﻿using System;
using System.Collections.Generic;
using System.Linq;
using Beyondsoft.Tuanju.DataManager;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;

namespace Tuan
{
    public sealed partial class ItemsPage : Tuan.Common.LayoutAwarePage
    {
        /// <summary>
        /// Initialize ItemsPage
        /// </summary>
        public ItemsPage()
        {
            this.InitializeComponent();
        }

        /// <summary>
        /// Binding context Data when loading this page
        /// </summary>
        /// <param name="navigationParameter">Navigation parameter</param>
        /// <param name="pageState">Page state</param>
        protected override async void LoadState(Object navigationParameter, Dictionary<String, Object> pageState)
        {
            this.CityName.DataContext = City.DefaultCity;

            DealGroup dealGroup = await DealGroup.GetDealGroupInstance();

            DealGroup group = dealGroup.Groups[(Category)navigationParameter];

            IEnumerable<Deal> groupDeals = await City.DefaultCity.GetDeals((Category)navigationParameter);

            this.DefaultViewModel["Group"] = group;

            if (groupDeals.Count() == 0)
            {
                this.NoDataWarnCanvas.Visibility = Visibility.Visible;
            }
            else
            {
                this.DefaultViewModel["Items"] = groupDeals;
            }
        }

        /// <summary>
        /// Navigating to ItemDetailsPage when click some deal
        /// </summary>
        /// <param name="sender">Grid view item</param>
        /// <param name="e">Selected view item</param>
        private void DealsGridView_ItemClick(object sender, ItemClickEventArgs e)
        {
            var itemId = ((Deal)e.ClickedItem).Gid;
            this.Frame.Navigate(typeof(ItemDetailsPage), itemId);
        }
    }
}
